package com.zhoumy.interview.study.thread;

/**
 * MultiThreadSingletonDemo（多线程的 单例模式）
 *
 * @author: zhoumy
 * @create: 2024年6月3日16:48:58
 */
public class MultiThreadSingletonDemo {

    private static MultiThreadSingletonDemo instance = null;

    private MultiThreadSingletonDemo () {
        System.out.println(Thread.currentThread().getName() + "\t 我是构造方法SingletonDemo");
    }

    public synchronized static MultiThreadSingletonDemo getInstance() {
        if(instance == null) {
            instance = new MultiThreadSingletonDemo();
        }
        return instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                MultiThreadSingletonDemo.getInstance();
            }, String.valueOf(i)).start();
        }
    }
}
